package com.example.touristassist;

import com.example.xmppcore.XMPPLogic;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;

import android.location.Location;
import android.os.Bundle;
//import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v4.app.FragmentActivity;

public class MenuActivity extends FragmentActivity implements
					GooglePlayServicesClient.ConnectionCallbacks,
					GooglePlayServicesClient.OnConnectionFailedListener,
					LocationListener {
	final float WalkSpeedKmH = 1;
	LocationClient lc;
	//int users_id;
	String username;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);
		
		lc = new LocationClient(this, this, this);
		
		username = XMPPLogic.getInstance().getConnection().getUser();
		//Bundle extra = getIntent().getExtras();
		//users_id = extra.getInt("users_id");
		//username = extra.getString("username");
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		lc.connect();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		lc.disconnect();
		super.onPause();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
			case R.id.menu_map:
				Location l = getCurrentLocationString();
		    	Intent intent = new Intent(this, MapActivity.class);
				String[] s = new String[] { username + ";" + String.valueOf(l.getLatitude()) + ";" + String.valueOf(l.getLongitude()) };
				intent.putExtra("location",s);
				startActivityForResult(intent, 1);
				break;
			case R.id.menu_exit:
				finish();
				android.os.Process.killProcess(android.os.Process.myPid());
				break;
		}
		
		return super.onOptionsItemSelected(item);
	}

	Location getCurrentLocationString() {
    	return lc.getLastLocation();
	}

	@Override
	public void onConnectionFailed(ConnectionResult connectionResult) {
		if (connectionResult.hasResolution()) {
            try {
                connectionResult.startResolutionForResult(this,9000);
            } catch (IntentSender.SendIntentException e) {
            	enableConnectivityService(false);
                Toast.makeText(this, e.getMessage(),Toast.LENGTH_SHORT);
            }
        } else {
        	enableConnectivityService(false);
            Toast.makeText(this, "Error get location.",Toast.LENGTH_SHORT);
        }
	}

	@Override
	public void onConnected(Bundle arg0) {
		enableConnectivityService(true);
	}

	@Override
	public void onDisconnected() {
		enableConnectivityService(false);
	}
	
	void enableConnectivityService(boolean enabled) {
		
	}

	@Override
	public void onLocationChanged(Location newLocation) {
		updateLocation(newLocation);
	}
	
	void updateLocation(Location location) {
		
	}
}
